package com.xhm.bbs.web.rest;

import com.xhm.bbs.pojo.dto.BbsAddDTO;
import com.xhm.bbs.pojo.dto.BbsUpdateDTO;
import com.xhm.bbs.pojo.mapper.BbsMapper;
import com.xhm.bbs.pojo.po.BbsPO;
import com.xhm.bbs.pojo.qo.BbsQO;
import com.xhm.bbs.pojo.vo.BbsListVO;
import com.xhm.bbs.pojo.vo.BbsShowVO;
import com.xhm.bbs.service.BbsService;
import com.xhm.bbs.web.AbstractController;
import com.xhm.bbs.web.api.BbsAPI;
import com.xhm.bbs.web.constant.WebConst;
import com.xhm.common.constant.ErrorCode;
import com.xhm.common.exception.BusinessException;
import com.xhm.common.pojo.qo.OptionQO;
import com.xhm.common.pojo.vo.OptionVO;
import com.xhm.common.pojo.vo.PageVO;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;
import javax.validation.Valid;

/**
 * 【帖子】控制器
 *
 * @author mrj
 * @date 2020/01/01
 */
@RestController
@RequestMapping(WebConst.API_PATH + "/bbs")
public class BbsController extends AbstractController implements BbsAPI {

    @Autowired
    private BbsService bbsService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<BbsShowVO> save(@Valid @RequestBody BbsAddDTO bbsAddDTO) throws Exception {
        BbsPO bbs = bbsService.save(bbsAddDTO);
        return ResponseEntity.created(new URI(WebConst.API_PATH +"/bbs/" + bbs.getId()))
            .body(BbsMapper.INSTANCE.toShowVO(bbs));
    }

    @Override
    @PutMapping
    public ResponseEntity<BbsShowVO> update(@Valid @RequestBody BbsUpdateDTO bbsUpdateDTO) {
        BbsPO bbs = bbsService.update(bbsUpdateDTO);
        return ResponseEntity.ok(BbsMapper.INSTANCE.toShowVO(bbs));
    }

    @Override
    @GetMapping
    public ResponseEntity<PageVO<BbsListVO>> list(@Valid BbsQO bbsQO) {
        PageVO<BbsListVO> page = bbsService.list(bbsQO);
        return ResponseEntity.ok(page);
    }

    @Override
    @GetMapping(value = "/options")
    public ResponseEntity<List<OptionVO<Long, String>>> findOptions(OptionQO<Long, String> qo) {
        List<OptionVO<Long, String>> options = bbsService.findOptions(qo);
        return ResponseEntity.ok(options);
    }

    @Override
    @GetMapping(value = "/{id}")
    public ResponseEntity<BbsShowVO> show(@PathVariable Long id) {
        BbsShowVO bbsShowVO = bbsService.show(id);
        return ResponseEntity.ok(bbsShowVO);
    }

    @Override
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Integer> delete(@PathVariable Long id) {
        int count = bbsService.delete(id);
        return ResponseEntity.ok(count);
    }

    @Override
    @DeleteMapping
    public ResponseEntity<Integer> deleteBatch(@RequestBody Long[] id) {
        if(ArrayUtils.isEmpty(id)){
            throw new BusinessException(ErrorCode.PARAM_IS_NULL);
        }
        int count = bbsService.delete(id);
        return ResponseEntity.ok(count);
    }


}


